package com.icss.ah.util;

import java.util.Iterator;
import java.util.Map;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;

public class HttpClientUtil {
	//static Logger logger = LogManager.getLogger(HttpClientUtil.class);
	private static String DEFAULT_CHARSET = "UTF-8";
	private static int DEFAULT_CONNECTION_TIMEOUT = 10 * 1000;
	private static int DEFAULT_SO_TIMEOUT = 10 * 1000;
	public static String addUrl(String head, String tail) {
		if (head.endsWith("/")) {
			if (tail.startsWith("/")) {
				return head.substring(0, head.length() - 1) + tail;
			} else {
				return head + tail;
			}
		} else {
			if (tail.startsWith("/")) {
				return head + tail;
			} else {
				return head + "/" + tail;
			}
		}
	}

	/**
	 * post请求数据
	 * @param url
	 * @param params
	 * @param charset
	 * @return
	 * @throws Exception
	 */
	public synchronized static String postData(String url, String json, String charset) {
		// 构造HttpClient的实例
		HttpClient httpClient = new HttpClient();
		// 创建post方法的实例
		PostMethod method = new PostMethod(url);
		((PostMethod) method).addParameter("json", json);
		HttpMethodParams httpMethodParam = method.getParams();
		httpMethodParam.setContentCharset("UTF-8");
		httpMethodParam.setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET,
				charset);
		String result = "";
		try {
			httpClient.executeMethod(method);// 执行postMethod
			result = new String(method.getResponseBody(), charset);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			// 释放连接
			method.releaseConnection();
		}
		return result;
	}
	
	public synchronized static String postData(String url, Map<String, String> header, String json, String charset) {
		// 构造HttpClient的实例
		HttpClient httpClient = new HttpClient();
		// 创建post方法的实例
		PostMethod method = new PostMethod(url);

		((PostMethod) method).addParameter("json", json);
		HttpMethodParams httpMethodParam = method.getParams();
		httpMethodParam.setContentCharset("UTF-8");
		httpMethodParam.setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, charset);
		// 添加请求头
		Iterator<String> iter = header.keySet().iterator();
		String key = null;
		while (iter.hasNext()) {
			key = iter.next();
			method.addRequestHeader(key, header.get(key));
		}
		String result = "";
		try {
			httpClient.executeMethod(method);// 执行postMethod
			result = new String(method.getResponseBody(), charset);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			// 释放连接
			method.releaseConnection();
		}
		return result;
	}
	
	/**
	 *  get请求数据
	 * @param url
	 * @param params
	 * @param charset
	 * @return
	 * @throws Exception
	 */
	public synchronized static String getData(String url, Map<String, String> params, String charset) {
		final HttpClient httpClient = new HttpClient();
		httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(DEFAULT_CONNECTION_TIMEOUT);
		httpClient.getHttpConnectionManager().getParams().setSoTimeout(DEFAULT_SO_TIMEOUT);
		final GetMethod method = new GetMethod(url);
		String result = "";
		try {
			httpClient.executeMethod(method);
			result = new String(method.getResponseBody(), charset);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			method.releaseConnection();
		}
		return result;
	}
}
